package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.dao.core.UserDiscountStatDao;
import com.tbit.uqbike.client.pojo.newEntity.UserDiscountStat;
import com.tbit.uqbike.client.service.UserDiscountStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 用户优惠享受统计
 * @Date: 创建于 15:43 2022/7/19
 */
@Service
public class UserDiscountStatServiceImpl implements UserDiscountStatService {

    @Autowired
    private UserDiscountStatDao userDiscountStatDao;

    @Override
    public void insert(UserDiscountStat userDiscountStat) {
        userDiscountStat.setUpdateTime(new Date());
        UserDiscountStat userDiscountStatDB = getByUserId(userDiscountStat.getUserId(), userDiscountStat.getAdAccountId(), userDiscountStat.getType());
        if (userDiscountStatDB == null) {
            userDiscountStat.setNum(1);
            userDiscountStatDao.insert(userDiscountStat);
        } else {
            userDiscountStat.setNum(userDiscountStatDB.getNum() + 1);
            userDiscountStatDao.edit(userDiscountStat);
        }

    }

    @Override
    public UserDiscountStat getByUserId(Integer userId, Integer adAccountId, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("adAccountId", adAccountId);
        map.put("type", type);
        return userDiscountStatDao.getByUserId(map);
    }
}
